package com.glela.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.alibaba.fastjson.JSONObject;
import com.glela.api.pay.yeepay.component.YeePay;
import com.glela.api.pay.yeepay.model.YeePayAliModel;
import com.glela.api.pay.yeepay.model.YeePayBaseRequest;
import com.glela.api.pay.yeepay.model.YeePayNetSilverModel;
import com.glela.api.pay.yeepay.model.YeePayRefundModel;
import com.glela.api.pay.yeepay.model.YeePayWeChatModel;
import com.glela.api.upload.qiniu.general.QiniuUtils;
import com.glela.common.constant.PayTypeEnum;
import com.glela.common.util.Config;
import com.glela.common.util.HttpUtil;
import com.glela.common.util.JsonUtil;
import com.glela.common.util.NetworkUtil;
import com.glela.common.util.OrderUtil;
import com.glela.order.mapper.OrderObjectMapper;
import com.glela.order.mapper.OrderPayMapper;
import com.glela.order.mapper.OrderSettlementCommisionMapper;
import com.glela.order.model.AfterSales;
import com.glela.order.model.OrderObject;
import com.glela.order.model.OrderPay;
import com.glela.order.model.OrderSettlementCommision;
import com.glela.order.service.AfterSalesService;
import com.glela.order.service.OrderService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.util.ParameterUtils;
import com.glela.statistics.service.UserEarningsService;

@Controller
@RequestMapping("yeePay")
public class YeePayController {
	
	private static Logger logger = LoggerFactory.getLogger(YeePayController.class);
	private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		
	@Resource(name = "orderService2")
	private OrderService orderService2;
	
	@Autowired
	private AfterSalesService afterSalesService;
	@Autowired
	private OrderSettlementCommisionMapper orderSettlementCommisionMapper;
	@Autowired
	private YeePay yeePay;
	@Autowired
	private OrderObjectMapper orderObjectMapper;
	@Autowired
	private OrderPayMapper orderPayMapper;
	@Autowired
	private BaseRestrictionsService baseRestrictionsService;
	@Autowired
	private UserEarningsService userEarningsService;
	/**
	 * 将支付二维码上传至七牛
	 * @param hexImageString
	 * @param orderSn
	 * @return
	 */
	public String uploadQRCode(String hexImageString, String orderSn){
		String qrUrl = "";
		byte[] b = hex2byte(hexImageString);
		qrUrl = QiniuUtils.uploadOrderQRCode("file/yeePay/", b, "jpg", orderSn);
		return qrUrl;
	}
	
	/**
	 * 易宝支付通用接口
	 * @param yeePayBaseRequest
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/yeePayExec", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> yeePayExec(@RequestBody YeePayBaseRequest yeePayBaseRequest,HttpServletRequest request) {
		String result="";
		try {
			if (ParameterUtils.validParams(yeePayBaseRequest.getOrderSn(),yeePayBaseRequest.getPayType()))
			{
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			
			OrderObject orderObject=getOrderService().selectOrderObjectByOrderSn(yeePayBaseRequest.getOrderSn());
			if (orderObject==null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20802), HttpStatus.OK);
			}
			YeePayNetSilverModel yeePaySilver = new YeePayNetSilverModel();
			// 网银参数
			yeePaySilver.setPayproducttype("ONEKEY");//固定值
			yeePaySilver.setUserno(orderObject.getUserId()+""); // 商户的用户识别 userID
			
			if(orderObject.getTotalFee().intValue()<=0)
			{
				orderObject.setTotalFee(1);
			}
			// 基础参数
			yeePaySilver.setRequestid(orderObject.getOrderSn()); // 订单号
			yeePaySilver.setAmount((((double)orderObject.getTotalFee())/100)+""); // 支付金额（元）
			yeePaySilver.setAssure("0"); // 是否担保  0否  1是
			yeePaySilver.setProductname(orderObject.getOrderSn()); // 商品名称 可选 
			yeePaySilver.setProductdesc(orderObject.getOrderSn()); // 商品描述
			yeePaySilver.setDivideinfo(""); // 分账信息
			yeePaySilver.setCallbackurl(Config.getString("yg-c-platform.deploy.url")+"/order/yeePayNetServerNotify"); // 点对点回调地址
			yeePaySilver.setWebcallbackurl( Config.getString("yg-c-platform.deploy.url")+"/order/yeePayWebCallBack" ); // 页面通知地址   为空则默认转跳到后台地址
			yeePaySilver.setPeriod(""); // 单位 ：天；当assure=1 时必填， 最大值：30  担保有效期
			yeePaySilver.setOrderexpdate("120"); // 订单支付有效时间
			
			yeePaySilver.setIp(NetworkUtil.getRealIp(request));
			yeePaySilver.setDirectcode("WAP-ZHT");
			
			result = yeePay.yeePayTradeNetSilver(yeePaySilver);
			JSONObject requestJson = JSONObject.parseObject(result);
			int ooResult=getOrderService().updateOrderObjectPayUrlById(orderObject.getId(), requestJson.getString("payurl"));
			if(ooResult<1)
			{
				logger.error("result="+result);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(requestJson), HttpStatus.OK);
		}  catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage()+"result="+result, e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	@RequestMapping(value = "/yeePayAli", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> yeePayAli(@RequestBody YeePayBaseRequest yeePayBaseRequest,HttpServletRequest request) {
		String result="";
		try {
			if (ParameterUtils.validParams(yeePayBaseRequest.getOrderSn(),yeePayBaseRequest.getPayType()))
			{
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			
			OrderObject orderObject=getOrderService().selectOrderObjectByOrderSn(yeePayBaseRequest.getOrderSn());
			if (orderObject==null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20802), HttpStatus.OK);
			}
			YeePayAliModel aliModel = new YeePayAliModel();
			// 支付宝额外提供IP地址
			// aliModel.setIp(request.getRemoteAddr());
			aliModel.setIp(NetworkUtil.getRealIp(request));
			aliModel.setDirectcode("WAP_ALIPAYAPP");
			
			if(orderObject.getTotalFee().intValue()<=0)
			{
				orderObject.setTotalFee(1);
			}
			// 基础参数
			aliModel.setRequestid(orderObject.getOrderSn()); // 订单号
			aliModel.setAmount((((double)orderObject.getTotalFee())/100)+""); // 支付金额（元）
			aliModel.setAssure("0"); // 是否担保  0否  1是
			aliModel.setProductname(orderObject.getOrderSn()); // 商品名称 可选 
			aliModel.setProductdesc(orderObject.getOrderSn()); // 商品描述
			aliModel.setDivideinfo(""); // 分账信息
			aliModel.setCallbackurl(Config.getString("yg-c-platform.deploy.url")+"/order/yeePayNetServerNotify"); // 点对点回调地址
			aliModel.setWebcallbackurl(""); // 页面通知地址   为空则默认转跳到后台地址
			aliModel.setPeriod(""); // 单位 ：天；当assure=1 时必填， 最大值：30  担保有效期
			aliModel.setOrderexpdate("120"); // 订单支付有效时间
			result = yeePay.yeePayTradeAlipay(aliModel);
			JSONObject requestJson = JSONObject.parseObject(result);
			int ooResult=getOrderService().updateOrderObjectPayUrlById(orderObject.getId(), requestJson.getString("payurl"));
			if(ooResult<1)
			{
				logger.error("yeePayAli result="+result);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(requestJson), HttpStatus.OK);
		}  catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage()+"yeePayAli result="+result, e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 易宝扫码支付
	 * @param yeePayBaseRequest
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/weChatQR", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> weChatQR(@RequestBody YeePayBaseRequest yeePayBaseRequest,HttpServletRequest request) {
		String result="";
		try {
			if (ParameterUtils.validParams(yeePayBaseRequest.getOrderSn(),yeePayBaseRequest.getPayType()))
			{
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			
			OrderObject orderObject=getOrderService().selectOrderObjectByOrderSn(yeePayBaseRequest.getOrderSn());
			if (orderObject==null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20802), HttpStatus.OK);
			}
			
			YeePayWeChatModel weChatModel = new YeePayWeChatModel();
			
			// 支付宝额外提供IP地址
			// weChatModel.setIp(request.getRemoteAddr());
			weChatModel.setIp(NetworkUtil.getRealIp(request));
			
			if(orderObject.getTotalFee().intValue()<=0)
			{
				orderObject.setTotalFee(1);
			}
			// 基础参数
			weChatModel.setRequestid(orderObject.getOrderSn()); // 订单号
			weChatModel.setAmount((((double)orderObject.getTotalFee())/100)+""); // 支付金额（元）
			weChatModel.setAssure("0"); // 是否担保  0否  1是
			weChatModel.setProductname(orderObject.getOrderSn()); // 商品名称 可选 
			weChatModel.setProductdesc(orderObject.getOrderSn()); // 商品描述
			weChatModel.setDivideinfo(""); // 分账信息
			weChatModel.setCallbackurl(Config.getString("yg-c-platform.deploy.url")+"/order/yeePayNetServerNotify"); // 点对点回调地址
			weChatModel.setWebcallbackurl(""); // 页面通知地址   为空则默认转跳到后台地址
			weChatModel.setPeriod(""); // 单位 ：天；当assure=1 时必填， 最大值：30  担保有效期
			weChatModel.setOrderexpdate("120"); // 订单支付有效时间

			JSONObject requestJson = null;
			int ooResult = 0;
			
			if (yeePayBaseRequest.getPayType() == PayTypeEnum.T20.getCode() ||
					yeePayBaseRequest.getPayType() == PayTypeEnum.T22.getCode()) {
				
				// 公众号支付时必须传OPENID
				if (yeePayBaseRequest.getPayType() == PayTypeEnum.T22.getCode()) {
					if (ParameterUtils.validParams(yeePayBaseRequest.getOpenId())) {
						return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20805), HttpStatus.OK);
					}
					weChatModel.setUserno(yeePayBaseRequest.getOpenId());
					// 废弃时间  2017-06-12 16:00
					// weChatModel.setPayproducttype("WECHATG");   旧支付通道参数已废弃
					// weChatModel.setDirectcode("WAP_WECHATG");   旧支付通道参数已废弃
					weChatModel.setPayproducttype("ONEKEY");
					weChatModel.setDirectcode("WAP_WECHATG");
				} else {
					weChatModel.setPayproducttype("WECHATAPP");
					weChatModel.setDirectcode("WAP_WECHATAPP");
				}
				result = yeePay.yeePayTradeWeChat(weChatModel);
				requestJson = JSONObject.parseObject(result);
				ooResult = getOrderService().updateOrderObjectPayUrlById(orderObject.getId(), requestJson.getString("payurl"));
			}
			
			if (yeePayBaseRequest.getPayType() == PayTypeEnum.T24.getCode()) {
				weChatModel.setPayproducttype("WECHATU");
				result = yeePay.yeePayTradeWeChatQRCode(weChatModel);
				requestJson = JSONObject.parseObject(result);
				
				BaseRestrictions baseRestrictions = baseRestrictionsService.getBaseRestrictionsForCache("yeepay_h5_pay_page");
				if(baseRestrictions == null || StringUtils.isBlank(baseRestrictions.getName())) {
					logger.error("未配置扫码支付页面: yeepay_h5_pay_page,请检查  BaseRestrictions" );
					return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
				}
				
				String qrpayurl = baseRestrictions.getName() + orderObject.getOrderSn();
				String qrUrl = QiniuUtils.getPayQrcode(qrpayurl, orderObject.getOrderSn());//自己生成二维码
				// 上传到七牛
//				String qrUrl = uploadQRCode(requestJson.getString("payurl"), orderObject.getOrderSn());
				requestJson.put("payurl", this.baseRestrictionsService.getBaseRestrictionsForCache("image_url").getName() + qrUrl);
				ooResult = getOrderService().updateOrderObjectPayUrlById(orderObject.getId(), qrUrl);
			}
			if (ooResult < 1) {
				logger.error("yeePayQR result=" + result);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(requestJson), HttpStatus.OK);
		}  catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage()+"yeePayAli result="+result, e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	@RequestMapping(value="/qcpay" ,method = RequestMethod.GET)
	public String qcpay(HttpServletRequest request,HttpServletResponse rep) {
		System.out.println("------------: "  + "=========" + request.getParameter("order_sn"));
		//rep.encodeRedirectURL("http://h5.xzhuang.com/shop/pay.html?orderSn=" + request.getParameter("order_sn"));
		return "支付错误,二维码已经过期";
		//return "redirect:http://h5.xzhuang.com/shop/pay.html?orderSn=" + request.getParameter("order_sn"); 
	}
	
	
	/**
	 * 易宝退款接口
	 * @param yeePayBaseRequest
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/yeePayRefund", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> yeePayRefund(@RequestBody YeePayBaseRequest yeePayBaseRequest, HttpServletRequest request) {
		try {
			logger.info("yeePayBaseRequest："+JsonUtil.toJson(yeePayBaseRequest));
			if (ParameterUtils.validParams(yeePayBaseRequest.getAfterSn())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			
			AfterSales afterSales = getOrderService().selectAfterSalesByCustomerServiceNumber(yeePayBaseRequest.getAfterSn());
			if (afterSales == null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20802), HttpStatus.OK);
			}
			/**
			 * 2017-12-17 gwq 直接退款 不管售后单的状态和是否入库
			 */
			if (yeePayBaseRequest.getIsGod() == 1) {//一般都是人工干预了退货状态 才会走强制退款
				afterSales.setIsRefund(1);
			}
			if (afterSales.getStatus() == 1) {
				// 请联系工作人员同意退款后再来
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C21205), HttpStatus.OK);
			}


			if (afterSales.getStatus() == 3) {
				// 当前订单已经成功售后  请不要重复退钱  不给退的
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C21206), HttpStatus.OK);
			}
			
			if (afterSales.getStatus() == 4) {
				// 被拒绝了  不给退钱
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C21207), HttpStatus.OK);
			}
			
			OrderObject orderObject = getOrderService().selectOrderObjectByOrderObjectId(afterSales.getOrderObjectId());
			if (orderObject == null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20802), HttpStatus.OK);
			}
			
			// 总金额
			long totel = orderObject.getTotalFee().longValue();
			// 现在退款的金额
			long nowMoney = afterSales.getRefundMoney();
			// 已经成功退款的金额
			long successMoney = afterSalesService.getRefundSuccessMoney(orderObject.getId());

			// 表示全部退款的金额
			if (totel - successMoney <= 0 || nowMoney == 0) {
				afterSales.setRefundId("当前售后订单的实际支付金额为0");
				afterSalesService.userRefundSuccess(afterSales);
//				userEarningsService.CalculateEarningsAfterSale(afterSales.getOrderDetailId(),afterSales.getOrderObjectId());
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
			}
			
			// 查询分账信息
			String divideString = getOrderService().selectDivideString(afterSales.getOrderDetailId());
			
			if ("FAIL".equals(divideString)) {//未分账 直接去请求分账
				getOrderService().divide(orderObjectMapper.selectOrderObjectById(afterSales.getOrderObjectId()).getOrderSn());
				divideString = getOrderService().selectDivideString(afterSales.getOrderDetailId());
				if ("FAIL".equals(divideString)) {//分账未成功
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C21211), HttpStatus.OK);
				}
			}
			
			logger.info("------->Divide Args Complete:" + divideString);

			OrderSettlementCommision orderSettlementCommision=
					orderSettlementCommisionMapper.selectByorderID(afterSales.getOrderObjectId());
			if(orderSettlementCommision!=null&&orderSettlementCommision.getStatus()!=4){//订单相关未处理
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C21211), HttpStatus.OK);
			}
			// 构建请求易宝的参数
			List<OrderPay> PayList=
				orderPayMapper.selectPaySuccessByOrderSn(orderObject.getOrderSn(),orderObject.getPayType());
			if(PayList!=null&&PayList.size()>0&&afterSales.getRefundMoney()!=null&&afterSales.getRefundMoney()>0){
				OrderPay OrderPay=PayList.get(0);
				String appKey = this.baseRestrictionsService.getBaseRestrictionsForCache("app_key").getName();
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("amount", ((afterSales.getRefundMoney().doubleValue()) / 100) + "");
				map.put("orderrequestid", OrderPay.getPaySn());
				map.put("requestid", afterSales.getCustomerServiceNumber());
				map.put("divideinfo", divideString);
				map.put("memo", "售后仅退货款");
				map.put("method", "yeepay.trade.refund");
				map.put("app_key", appKey);
				map.put("format", "json");
				map.put("timestamp", sdf.format(new Date()));
				String requestUrl = baseRestrictionsService.getBaseRestrictionsForCache("yeeRefund_url").getName();
				
				//开始请求
				JSONObject resultJson = JSONObject.parseObject(HttpUtil.post(requestUrl,map));
				String result=JsonUtil.toJson(resultJson);
				
				if (resultJson.getJSONObject("error_response") != null) {
					return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000,resultJson.getJSONObject("error_response").getString("msg")), HttpStatus.OK);
				}
				
				if ("1".equals(resultJson.getJSONObject("yeepay_trade_refund_response").getString("code"))) {
					afterSales.setRefundId(resultJson.getJSONObject("yeepay_trade_refund_response").getString("refundexternalid")); // 保存退款流水号
					afterSalesService.userRefundSuccess(afterSales);
//					userEarningsService.CalculateEarningsAfterSale(afterSales.getOrderDetailId(),afterSales.getOrderObjectId());
					return new ResponseEntity<>(new ResponseResultModel<>(result), HttpStatus.OK);
				} else {
					logger.error("--------->YeePay Refund Faild Response:" + result);
				}
				return new ResponseEntity<>(new ResponseResultModel<>(result), HttpStatus.OK);
			 }else if(PayList!=null&&PayList.size()>0){
				 afterSalesService.userRefundSuccess(afterSales);
//				 userEarningsService.CalculateEarningsAfterSale(afterSales.getOrderDetailId(),afterSales.getOrderObjectId());
			 }
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
		return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
	}
	
	/**
	 * 易宝退邮费
	 * @param yeePayBaseRequest
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/yeePayRefundPostage", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> yeePayRefundPostage(@RequestBody YeePayBaseRequest yeePayBaseRequest, HttpServletRequest request) {
		try {
			if (ParameterUtils.validParams(yeePayBaseRequest.getOrderSn())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			OrderObject orderObject = getOrderService().selectOrderObjectByOrderSn(yeePayBaseRequest.getOrderSn());
			if (orderObject == null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20802), HttpStatus.OK);
			}
			int goodsType = getOrderService().selectDetailGoodsType(orderObject.getId());
			// 构建请求易宝的参数
			YeePayRefundModel model = new YeePayRefundModel();
			String divide = getOrderService().selectSettlementAccount(orderObject.getId());
			model.setOrderrequestid(orderObject.getOrderSn()); // 请求退款的单号
			model.setAmount((orderObject.getFreight().doubleValue()/100) + "");
			model.setConfirm("1"); // 确认退款 固定值1
			model.setDivideinfo(divide + ":AMOUNT"+(orderObject.getFreight().doubleValue()/100)); // 不填写则默认从主账户上扣款
			if (goodsType == 0) {
				model.setRequestid("C" + OrderUtil.getOrderSn()); // 退款单号
				model.setMemo("C区商品售后退邮费"); // 商户备注
			}
			if (goodsType == 1) {
				model.setRequestid("B" + OrderUtil.getOrderSn()); // 退款单号
				model.setMemo("B区商品售后退邮费"); // 商户备注
			}
			
			String result = yeePay.yeePayRefund(model);
			logger.info("------->YeePay Response Args:" + result);
			JSONObject resultJson = JSONObject.parseObject(result);
			String refundCode = resultJson.getString("code");
			if (refundCode.equals("1")) {
				String refundFeeNo = resultJson.getString("refundexternalid");
				getOrderService().updateOrderObjectFreightNo(orderObject.getId(), refundFeeNo);
				return new ResponseEntity<>(new ResponseResultModel<String>(result), HttpStatus.OK);
			} else {
				logger.error("--------->YeePay Refund Faild Response:" + result);
			}
			return new ResponseEntity<>(new ResponseResultModel<>(result), HttpStatus.OK);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	private OrderService getOrderService() {
		return orderService2;
	}
	
	public byte[] hex2byte(String s) {
		byte[] src = s.toLowerCase().getBytes();
		byte[] ret = new byte[src.length / 2];
		for (int i = 0; i < src.length; i += 2) {
			byte hi = src[i];
			byte low = src[i + 1];
			hi = (byte) ((hi >= 'a' && hi <= 'f') ? 0x0a + (hi - 'a') : hi - '0');
			low = (byte) ((low >= 'a' && low <= 'f') ? 0x0a + (low - 'a') : low - '0');
			ret[i / 2] = (byte) (hi << 4 | low);
		}
		return ret;
	}
}